//
//  DragTableItemView.m
//  drawacartoon
//
//  Created by Ji Hu on 12-7-5.
//  Copyright (c) 2012年 爱辅导. All rights reserved.
//

#import "DragTableItemView.h"

#define Title_Label_Height 18

@implementation DragTableItemView

@synthesize sourceView,titleLabel=_titleLabel;
@synthesize panGestureRecongnizer;
@synthesize delegate=_delegate;
@synthesize name=_name;

-(void)dealloc{
    self.sourceView = nil;
    self.delegate = nil;
    self.name=nil;
    self.titleLabel=nil;
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
//        UIPanGestureRecognizer *pg = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
//        [pg setMaximumNumberOfTouches:2];
//        [pg setMinimumNumberOfTouches:2];
//        [self addGestureRecognizer:pg];
//        [pg release];
        
    }
    return self;
}

-(id)initWithView:(UIView *)view withFrame:(CGRect)frame{
    
    CGRect realRect = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height+Title_Label_Height);
    
    self = [self initWithFrame:realRect];
    
    if (self) {
        self.sourceView = view;
        [self addSubview:sourceView];
        self.backgroundColor = [UIColor clearColor];
        [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
        [self setClipsToBounds:NO];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height-Title_Label_Height, self.frame.size.width, Title_Label_Height)];
        _titleLabel.textAlignment=UITextAlignmentCenter;
        [self addSubview:_titleLabel];
    }
    
    return self;
}

-(void)setName:(NSString *)name{
    if (_name) {
        [_name release];
        _name = nil;
    }
    
    _name = [name retain];
    
    _titleLabel.text=name;
}

//-(void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer{
//    
////    if([gestureRecognizer numberOfTouches]<=1) return;
//    
////    CGPoint point = [gestureRecognizer locationOfTouch:0 inView:self];
//    
//    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
//       
//    }
//    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
//        NSLog(@"gesture end.");  
//    }else if ( gestureRecognizer.state == UIGestureRecognizerStateCancelled){
//        NSLog(@"gesture cancel.");  
//    }
//    else if ( gestureRecognizer.state == UIGestureRecognizerStateRecognized){
//        NSLog(@"gesture recognized.");  
//    }
//    else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) {
//      
//    }
//}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    if ([event allTouches].count>1) {
        return;
    }
    
    UITouch *touch=[touches anyObject];
    timeStamp = [touch timestamp];
    
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
    if ([event allTouches].count>1) {
        return;
    }
    
    UITouch *touch=[touches anyObject];
    
    CGPoint to = [touch locationInView:self];
    CGPoint last = [touch previousLocationInView:self];
    
    float dx=(to.x-last.x);
    float dy=(to.y-last.y);
    
    if (self.frame.origin.x+dx<0) {
        dx = - self.frame.origin.x;
    }
    
    if (self.frame.origin.y+dy<0) {
        dy = - self.frame.origin.y;
    }
    
    if (self.frame.origin.x+dx>self.superview.bounds.size.width) {
        dx = self.superview.bounds.size.width -self.frame.origin.x;
    }
    
    if (self.frame.origin.y+dy>self.superview.bounds.size.height) {
        dy = self.superview.bounds.size.height -self.frame.origin.y;
    }
    
    [self setFrame:CGRectMake(self.frame.origin.x+dx, self.frame.origin.y+dy, self.frame.size.width, self.frame.size.height)];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[touches anyObject];
   
    if ([touch timestamp]-timeStamp<0.2) {
        [self.delegate itemClick:self];
    }
    
}

@end
